from rest_framework import serializers

from news.models import News, NewsComments, NewsCategory
from users.models import User

# 首页部分


class UserSerializer(serializers.ModelSerializer):
    """
    用户序列化器
    """
    class Meta:
        model = User
        fields = ('username', 'id')


class NewsCategorySerializer(serializers.ModelSerializer):
    """
    新闻分类序列化器
    """
    class Meta:
        model = NewsCategory
        fields = ("id", "category_name")


class NewsListSerializer(serializers.ModelSerializer):
    """
    新闻列表序列化器
    """
    user = UserSerializer()

    class Meta:
        model = News
        fields = ('id', 'title', 'user', 'content', 'news_image', 'create_time', 'clicks')


# 新闻详情页部分


class NewsSerializer(serializers.ModelSerializer):
    """
    单条新闻序列化器
    """
    user = UserSerializer()

    class Meta:
        model = News
        fields = ('title', 'user', 'content', 'news_time', )


class NewsCommentsSerializer(serializers.ModelSerializer):
    """
    文章评论表
    """
    user = UserSerializer()

    class Meta:
        model = NewsComments
        fields = ('user', 'comment', 'parent_id','id')


class UserTopNewsSerializer(serializers.ModelSerializer):
    """
    打开的页面文章作者新闻展示
    """
    class Meta:
        model = News
        fields = ('id', 'title', 'clicks')
